$(function() {
	changeHeaderMenu(1);
	loadFirmwareInfo();
	initTable();
	$('#find_device_status').change(btn_find);
	$('#find_device_model').change(btn_find);
});

function loadFirmwareInfo() {
	$.getJSON('/cloud_ifw/member/firmwareinfobrow_getlist.action', function(data) {
		if(data.success && data.result)
			data = data.result;
		var html = '';
		$.each(data.rows, function(i, r) {
			html += '<option value="' + r.deviceVersion + '">' + r.deviceVersion + '(' + r.deviceModel + ')</option>';
		});
		$('#find_device_version').html(html);
	});
}

/**
 * 初始化表格数据
 */
function initTable() {
	$('#grid').grid({
		title : '',
		pager : true,
		multiSelect: true,
		checkbox: true,
		tbar : '#navbar',
		store : {
			url : '/cloud_ifw/member/deviceinfobrow_getlist.action',
			idProperty : 'deviceIndex',
			uploadPagesize : 'rows'
		},
		pageCfg : {
			btnCount : 5
		},
		callback_inited: function() {
			$('#grid thead').hide();
		},
		columns : [
	        {cn:'路由名称/型号/版本号/备注', en:'deviceName', width : 250, tpl: function(r) {
	        	var html = '';
	        	var deviceSn = r.deviceSn;
	        	var data = $.ajax({url: "/cloud_ifw/member/deviceinfobrow_getsnbymap.action?deviceSn=" + r.deviceSn, async: false }).responseText;
	        	data = $.parseJSON(data);
	        	if(data.success && data.result)
	        		deviceSn = data.result;
	        	html += '设备编号/名称：' + deviceSn;
	        	if(r.deviceName && r.deviceName != 'NONE') {
	        		html += '<br/>';
	        		html += '网吧名称：' + r.deviceName + '';
	        	}
//	        	html += '<br/>';
//	        	html += '型号：' + r.deviceModel;
	        	html += '<br/>';
	        	html += '版本号：' + r.deviceVersion;
	        	html += '<br/>';
	        	html += '<div class="note">备注：';
	        	html += '<span class="note_content"><span class="note_val">' + (r.note?r.note:'无') + '</span>&nbsp;<img src="images/edit.png" style="width: 20px; height: 20px; cursor: pointer;" onclick="note_edit(this)"/></span>';
	        	html += '<input type="text" class="note_edit_input" style="display: none;"/>';
	        	html += '&nbsp;<button type="button" class="note_edit_ok" style="display: none;" onclick="note_edit_ok(this, ' + r.deviceIndex + ')">保存</buttion>';
        		html += '&nbsp;<button type="button" class="note_edit_cancel" style="display: none;" onclick="note_edit_cancel(this)">取消</buttion></div>';
	        	return html;
	        }},
//	        {cn:'型号', en:'deviceModel',	width : 80},
//	        {cn:'版本号', en:'deviceVersion',	width : 120},
//	        {cn:'所属用户', en:'deviceBindCode',	width : 150},
	        {cn:'状态', en:'deviceActiveLast', width : 60, tpl: function(r) {
	        	if(r && r.deviceActiveLast) {
	        		var date = new Date();
	        		var t_now = date.getTime();
	        		var t = r.deviceActiveLast.time;
	        		if(t_now - t < 300000)
	        			return '<span style="color: green;">在线</span>'; 
	        	}
	        	return '<span style="color: red;">离线</span>';
	        }},
	        {cn:'IP/到期时间', en:'deviceWanIp1',width : 200, tpl: function(r) {
	        	var html = '';
	        	if(!r.deviceWanIp1)
	        		r.deviceWanIp1 = '0.0.0.0';
	        	html += 'WAN口IP：' + r.deviceWanIp1;
	        	html += '<br/>';
	        	html += '到期时间：' + r.deviceExpireTime;
	        	return html;
	        }},
//	        {cn:'到期时间', en:'deviceExpireTime',width : 200},
	        {cn:'在线主机/上下行速率', en:'deviceOnline',width : 200, tpl: function(r) {
	        	var html = '';
	        	if(!r.deviceOnline)
	        		r.deviceOnline = 0;
	        	html += '在线主机：' + r.deviceOnline;
	        	html += '<br/>';
	        	html += '上/下行速率：';
	        	if(r && r.deviceTxTotal)
	        		html += toNetSpeed(r.deviceTxTotal);
	        	else
	        		html += '0';
        		html += '&nbsp;/&nbsp;';
	        	if(r && r.deviceRxTotal)
	        		html += toNetSpeed(r.deviceRxTotal);
	        	else
	        		html += '0';
	        	return html;
	        }},
//	        {cn:'上行速率', en:'deviceTxTotal',width : 100, tpl: function(r) {
//	        	if(r && r.deviceTxTotal)
//	        		return toNetSpeed(r.deviceTxTotal);
//	        	return r.deviceTxTotal;
//	        }},
//	        {cn:'下行速率', en:'deviceRxTotal',width : 100, tpl: function(r) {
//	        	if(r && r.deviceRxTotal)
//	        		return toNetSpeed(r.deviceRxTotal);
//	        	return r.deviceRxTotal;
//	        }},
//	        {cn:'上/下行速率', en:'deviceTxTotal',width : 150, tpl: function(r) {
//	        	var html = '';
//	        	html += '上行：';
//	        	if(r && r.deviceTxTotal)
//	        		html += toNetSpeed(r.deviceTxTotal);
//	        	else
//	        		html += '0';
//        		html += '<br/>';
//	        	html += '下行：';
//	        	if(r && r.deviceRxTotal)
//	        		html += toNetSpeed(r.deviceRxTotal);
//	        	else
//	        		html += '0';
//	        	return html;
//	        }},
	        {cn:'操作', en:'deviceIndex',width : 150, tpl: function(r) {
	        	var html = '';
	        	html += '<button onclick="btn_detail(' + r.deviceIndex + ')" class="btn btn-default">详情</button>';
	        	html += '&nbsp;';
	        	html += '<button onclick="btn_del(\'' + r.deviceSn + '\')" class="btn btn-default">删除</button>';
	        	if(r && r.deviceActiveLast) {
	        		var date = new Date();
	        		var t_now = date.getTime();
	        		var t = r.deviceActiveLast.time;
	        		if(t_now - t < 300000) {
	        			html += '<br/>';
	        			html += '<div style="height: 5px;"></div>';
	        			html += '<button onclick="btn_remote(' + r.deviceIndex + ')" class="btn btn-default">远控</button>';
	        			html += '&nbsp;';
	        			html += '<button onclick="btn_reboot(\'' + r.deviceSn + '\')" class="btn btn-default">重启</button>';
	        		}
	        	}
	        	return html;
	        }}
		]
	});
}

function note_edit(obj) {
	$parent = $(obj).parent().parent();
	$parent.find('.note_content').hide();
	$parent.find('.note_edit_input').show();
	var note = $parent.find('.note_val').html();
	if(note == '无')
		note = '';
	$parent.find('.note_edit_input').val(note);
	$parent.find('.note_edit_ok').show();
	$parent.find('.note_edit_cancel').show();
}

function note_edit_ok(obj, deviceIndex) {
	$parent = $(obj).parent();
	var note = $parent.find('.note_edit_input').val();
	var note_old = $parent.find('.note_val').html();
	if(note_old == '无')
		note_old = '';
	if(note == note_old) {
		return;
	}
	var data = {
		deviceIndex: deviceIndex,
		note: note
	};
	$.getJSON('/cloud_ifw/member/deviceinfobrow_editnote.action', data, function(data) {
        if(data.success && data.result){
        	$parent.find('.note_val').html(note);
        }
        note_edit_cancel(obj);
	});
}

function note_edit_cancel(obj) {
	$parent = $(obj).parent();
	$parent.find('.note_content').show();
	$parent.find('.note_edit_input').hide();
	$parent.find('.note_edit_ok').hide();
	$parent.find('.note_edit_cancel').hide();
}

function btn_find() {
	var data = {};
	var deviceStatus = $('#find_device_status').val();
	if(deviceStatus)
		data["deviceStatus"] = deviceStatus;
	var deviceModel = $('#find_device_model').val();
	if(deviceModel)
		data["deviceModel"] = deviceModel;
	var deviceSn = $('#find_device_sn').val();
	if(deviceSn)
		data["deviceSn"] = deviceSn;
	$('#grid').grid('load', {data : data});
}

function btn_detail(id) {
	location.href = 'deviceDetail.jsp?id=' + id;
}

function btn_remote(id) {
	var layer_loading = layer.load(0, {shade: false});
	$.getJSON('/cloud_ifw/member/deviceinfobrow_remote.action', {
		deviceIndex: id
	}, function(data) {
		layer.close(layer_loading);
		if(data.success && data.result) {
			data = data.result;
			if(data && data != "0")
				setTimeout(function() {
					window.open('http://yun.ifw8.cn:' + data);
				}, 500);
			else
				alert('远控失败');
		} else {
			alert(data.result.errors);
		}
	});
}

function btn_reboot(deviceSn) {
	if(confirm("是否确定执行重启操作？")) {
		$.getJSON('/cloud_ifw/member/deviceinfobrow_rebootbydevicesn.action', {
			deviceSn: deviceSn
		}, function(data) {
			if(data.result)
				data = data.result;
			if(data == '1')
				alert('已执行重启');
			else
				alert(data.errors);
		});
	}
}

function btn_update() {
	var data = {};
	var deviceVersion = $('#find_device_version').val();
	data["deviceVersion"] = deviceVersion;
	var rs = $('#grid').grid('getSelectedRecords');
	if(!rs || !rs.length) {
		alert('请选择设备');
		return;
	}
	var ids = '';
	$.each(rs, function(i, r) {
		if(r && r.deviceActiveLast) {
			var date = new Date();
			var t_now = date.getTime();
			var t = r.deviceActiveLast.time;
			if(t_now - t < 300000)
				ids += ',' + r.deviceSn;
		}
	});
	if(ids) {
		ids = ids.substr(1);
	} else {
		alert('请选择在线设备');
		return;
	}
	data["ids"] = ids;
	if(confirm("是否确定执行升级操作？")) {
		$.getJSON('/cloud_ifw/member/deviceinfobrow_update.action', data, function(data) {
			if(data.result)
				data = data.result;
			if(data == '1')
				alert('已执行升级');
			else
				alert(data.errors);
		});
	}
}

function btn_updateandreboot() {
	var data = {};
	var deviceVersion = $('#find_device_version').val();
	data["deviceVersion"] = deviceVersion;
	var rs = $('#grid').grid('getSelectedRecords');
	if(!rs || !rs.length) {
		alert('请选择设备');
		return;
	}
	var ids = '';
	$.each(rs, function(i, r) {
		if(r && r.deviceActiveLast) {
			var date = new Date();
			var t_now = date.getTime();
			var t = r.deviceActiveLast.time;
			if(t_now - t < 300000)
				ids += ',' + r.deviceSn;
		}
	});
	if(ids) {
		ids = ids.substr(1);
	} else {
		alert('请选择在线设备');
		return;
	}
	data["ids"] = ids;
	if(confirm("是否确定执行升级并立即重启操作？")) {
		$.getJSON('/cloud_ifw/member/deviceinfobrow_updateandreboot.action', data, function(data) {
			if(data.result)
				data = data.result;
			if(data == '1')
				alert('已执行升级并立即重启');
			else
				alert(data.errors);
		});
	}
}

function btn_del(id) {
	btn_del_common(id, "此操作会将该设备从您的云平台下删除，确认？");
}

function btn_delbatch() {
	var rs = $('#grid').grid('getSelectedRecords');
	if(!rs || !rs.length) {
		alert('请选择设备');
		return;
	}
	var ids = '';
	$.each(rs, function(i, r) {
		ids += ',' + r.deviceSn;
	});
	if(ids) {
		ids = ids.substr(1);
	} else {
		alert('请选择设备');
		return;
	}
	btn_del_common(ids, "此操作会将选中设备从您的云平台下删除，确认？");
}

function btn_del_common(ids, text) {
	var data = {};
	data["ids"] = ids;
	if(confirm(text)) {
		$.getJSON('/cloud_ifw/member/deviceinfobrow_del.action', data, function(data) {
			if(data.result)
				data = data.result;
			if(data == '1') {
				alert('删除成功');
				btn_find();
			} else {
				alert(data.errors);
			}
		});
	}
}